package com.example.demo.service.imp;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Flower;
import com.example.demo.entity.Myorder;
import com.example.demo.mapper.OrdesrMapper;
import com.example.demo.service.OrdersService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrdersServiceImp implements OrdersService {

    @Resource
    private OrdesrMapper ordesrMapper;


    @Override
    public void insert(Myorder myorder) {
        ordesrMapper.insert(myorder);
    }

    @Override
    public void updateById(Myorder myorder) {
        ordesrMapper.updateById(myorder);
    }

    @Override
    public void deleteById(Long id) {
        ordesrMapper.deleteById(id);
    }

    @Override
    public Page<Myorder> selectPage(Integer pageNum, Integer pageSize, String search,String state,String username) {
        Myorder myorder = new Myorder();
        LambdaQueryWrapper<Myorder> wrapper = Wrappers.<Myorder>lambdaQuery();
        if (StrUtil.isNotBlank(search) ) {
            wrapper.eq(Myorder::getOrderId, search).or();
        }
        if (StrUtil.isNotBlank(username)) {
            wrapper.and(wrapper1->wrapper1.eq(Myorder::getUserName, username));
        }
        if (StrUtil.isNotBlank(state)) {
            wrapper.and(wrapper1->wrapper1.eq(Myorder::getOrderStatus, state));
        }
        Page<Myorder> page =ordesrMapper.selectPage(new Page<>(pageNum, pageSize),wrapper);
        List<Myorder> list =ordesrMapper.selectAll(myorder);
        for(int i=0;i<page.getRecords().size();i++){
            for(int j=0;j<list.size();j++){
                if(page.getRecords().get(i).getFlowerId().equals(list.get(j).getFlowerId())){
                    if(page.getRecords().get(i).getUserId().equals(list.get(j).getUserId())) {
                        page.getRecords().get(i).setUserName(list.get(j).getUserName());
                        page.getRecords().get(i).setFlowerName(list.get(j).getFlowerName());
                        page.getRecords().get(i).setImageUrl(list.get(j).getImageUrl());
                        ordesrMapper.updateById(page.getRecords().get(i));
                    }
                }
            }
        }
        return page;
    }

    @Override
    public void updateShipments(Myorder myorder) {
        if(myorder.getOrderStatus().equals("未发货")){
            myorder.setOrderStatus("已发货");
        }else if(myorder.getOrderStatus().equals("已发货")){
            myorder.setOrderStatus("已完成");
        }

        ordesrMapper.updateById(myorder);
    }

    @Override
    public Page<Myorder> mySelectPage(Integer pageNum, Integer pageSize, String search) {
        Myorder myorder = new Myorder();
        LambdaQueryWrapper<Myorder> wrapper = Wrappers.<Myorder>lambdaQuery();
        if (StrUtil.isNotBlank(search) ) {
            wrapper.like(Myorder::getUserId, search).or();
        }
        Page<Myorder> page =ordesrMapper.selectPage(new Page<>(pageNum, pageSize),wrapper);
        List<Myorder> list =ordesrMapper.selectAll(myorder);
        for(int i=0;i<page.getRecords().size();i++){
            for(int j=0;j<list.size();j++){
                if(page.getRecords().get(i).getFlowerId().equals(list.get(j).getFlowerId())){
                    if(page.getRecords().get(i).getUserId().equals(list.get(j).getUserId())) {
                        page.getRecords().get(i).setUserName(list.get(j).getUserName());
                        page.getRecords().get(i).setFlowerName(list.get(j).getFlowerName());
                        page.getRecords().get(i).setImageUrl(list.get(j).getImageUrl());
                        page.getRecords().get(i).setPrice(list.get(j).getPrice());
                    }
                }
            }
        }
        return page;
    }


}
